import { getOrderCompletionTimeLimitStartNodeOptions } from '@/api/order'
import { ref } from 'vue'

export const useGetCompletionTimeLimitStartNodeOptions = () => {
  const getCompletionTimeLimitStartNodeOptionsLoading = ref(false)
  const getCompletionTimeLimitStartNodeOptionsParam =
    ref<OrderSetAPIRequest.GetCompletionTimeLimitStartNodeOptionsParam>({
      ascriptionType: null,
      departmentId: '',
      id: '',
      limit: 50,
      name: '',
      page: 1,
    })
  const completionTimieLimitStartNodeOptions = ref<
    Array<OrderSetAPIResponse.CompletionTimeLimitStartNodeOption>
  >([])

  const getCompletionTimeLimitStartNodeOptions = async () => {
    try {
      getCompletionTimeLimitStartNodeOptionsLoading.value = true
      const res = await getOrderCompletionTimeLimitStartNodeOptions(
        getCompletionTimeLimitStartNodeOptionsParam.value,
      )
      getCompletionTimeLimitStartNodeOptionsLoading.value = false
      if (res.data.code === 20000) {
        completionTimieLimitStartNodeOptions.value = res.data.data.rows
      }
      return res.data
    } catch (error) {
      getCompletionTimeLimitStartNodeOptionsLoading.value = false
    }
  }

  return {
    completionTimieLimitStartNodeOptions,
    getCompletionTimeLimitStartNodeOptionsParam,
    getCompletionTimeLimitStartNodeOptionsLoading,
    getCompletionTimeLimitStartNodeOptions,
  }
}
